﻿using N_m3u8DL_RE.Common.Util;
using N_m3u8DL_RE.Entity;
using System.Collections.Concurrent;

namespace N_m3u8DL_RE.DownloadManager
{
    public class TaskInfo
    { 
        public int TaskId { get; set; }
        public TaskInfo(SpeedContainer speedContainer,int taskId)
        {
            this.speedContainer = speedContainer;
            TaskId = taskId;
        }
        
        public SpeedContainer speedContainer { get; }
        private ConcurrentDictionary<int, string> DateTimeStringDic = new();
        private ConcurrentDictionary<int, string> SizeDic = new();
        private long _stopSpeed = 0;
        public void Refresh()
        {
            var percentage = speedContainer.Value / speedContainer.MaxValue * 100;
            percentage = Math.Min(100, Math.Max(0, percentage));
            var sizeStr = "0MB/0MB";
            var now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            if (speedContainer.Value != 0)
            {
                var size = speedContainer.RDownloaded;
                // 一秒汇报一次即可
                if (DateTimeStringDic.TryGetValue(TaskId, out var oldTime) && oldTime != now)
                {
                    var totalSize = speedContainer.SingleSegment ? speedContainer.ResponseLength ?? 0 : (long)(size / (speedContainer.Value / speedContainer.MaxValue));
                    SizeDic[TaskId] = $"{GlobalUtil.FormatFileSize(size)}/{GlobalUtil.FormatFileSize(totalSize)}";
                }
                SizeDic.TryGetValue(TaskId, out sizeStr);
            }

            // 单文件下载汇报进度
            if (speedContainer is { SingleSegment: true, ResponseLength: not null })
            {
                speedContainer.MaxValue = (double)speedContainer.ResponseLength;
                speedContainer.Value = speedContainer.RDownloaded;
            }
            // 一秒汇报一次即可
            if (DateTimeStringDic.TryGetValue(TaskId, out var oldTime1) && oldTime1 != now)
            {
                speedContainer.NowSpeed = speedContainer.Downloaded;
                // 速度为0，计数增加
                if (speedContainer.Downloaded <= _stopSpeed) { speedContainer.AddLowSpeedCount(); }
                else speedContainer.ResetLowSpeedCount();
                speedContainer.Reset();
            }
            string speedStr = GlobalUtil.FormatFileSize(speedContainer.NowSpeed) + (speedContainer.LowSpeedCount > 0 ? $"({speedContainer.LowSpeedCount})" : "") + "/s";
            DateTimeStringDic[TaskId] = now;
            string percent = $"{percentage:F2}%";
            if(percent.Contains("NaN") || percent.Contains("∞"))
            {
                percent = "0.00%";
            }
            string frameSize = speedContainer.Resolution;
            if (string.IsNullOrEmpty(frameSize))
            {
                frameSize = "0x0";
            }
            string info = $"percent {speedContainer.Value}/{speedContainer.MaxValue} {percent} {sizeStr} {speedStr} {frameSize}";
            Console.WriteLine(info);
        }
    }
}
